package com.ctsi.ssdc.admin.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import com.zdww.biyi.component.sdk.aop.BeanExposeMethodAble;
import com.ctsi.ssdc.admin.domain.CscpOrg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ssdc.admin.domain.dto.CscpOrgCriteria;
import com.ctsi.ssdc.admin.domain.dto.CscpOrgDTO;
import com.ctsi.ssdc.admin.domain.dto.CscpOrgParamDTO;
import com.ctsi.ssdc.admin.service.CscpOrgService;
import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.AjaxBackData;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;

import static com.ctsi.ssdc.admin.consts.ComponentConstant.ADMIN;
import static com.ctsi.ssdc.admin.consts.ComponentConstant.METHOD;


/**
 * REST controller for managing CscpOrg.
 *
 * @author ctsi biyi generator
 *
 */
@RestController
@RequestMapping("/api/system")
public class CscpOrgController {

    private final Logger log = LoggerFactory.getLogger(CscpOrgController.class);

    private static final String ENTITY_NAME = "cscpOrg";

    private final CscpOrgService cscpOrgService;

    public CscpOrgController(CscpOrgService cscpOrgService) {
        this.cscpOrgService = cscpOrgService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
    
    /**
     * POST  /cscpOrgs : Create a new cscpOrg.
     *
     * @param cscpOrgDTO the cscpOrgDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cscpOrgDTO, or with status 400 (Bad Request) if the cscpOrg has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cscpOrgs")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpOrgDTO> createCscpOrg(@RequestBody CscpOrgDTO cscpOrgDTO) throws URISyntaxException {
        log.debug("REST request to save CscpOrg : {}", cscpOrgDTO);
        if (cscpOrgDTO.getId() != null) {
            throw new BadRequestAlertException("A new cscpOrg cannot already have an ID", ENTITY_NAME, "idexists");
        }
        CscpOrgDTO result = cscpOrgService.insert(cscpOrgDTO);
        return ResponseEntity.created(new URI("/api/cscpOrgs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
    
    /**
     * POST  /cscpOrgs : 保存数列，包括org列表，org_workGroup org_user;
     *
     * @param  cscpOrgDTO the cscpOrgDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cscpOrgDTO, or with status 400 (Bad Request) if the cscpOrg has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cscpOrgs/save")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public AjaxBackData saveCscpOrg(@RequestBody CscpOrgParamDTO cscpOrgParamDTO ) throws URISyntaxException {
        AjaxBackData ajaxBackData = new AjaxBackData(true, "");
        int id = cscpOrgService.save(cscpOrgParamDTO);
        ajaxBackData.setId(""+id);
        return ajaxBackData;
    }
    //删除机构 机构人员 等信息
    @DeleteMapping("/cscpOrgs/delCscpOrg")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public AjaxBackData delCsspOrg(@RequestBody CscpOrgParamDTO cscpOrgParamDTO ){
        AjaxBackData ajaxBackData = new AjaxBackData(true, "");
        cscpOrgService.deleteAll(cscpOrgParamDTO);
        return ajaxBackData;
    }

    @GetMapping("/cscpOrgs/all")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public CscpOrgParamDTO fetchCscpOrgs(Integer parentId) {
        log.debug("REST request to get CscpOrgs");
        return cscpOrgService.fetchCscpOrgsUpdate(parentId);
    }
    

    /**
     * PUT  /cscpOrgs : Updates an existing cscpOrg.
     *
     * @param cscpOrgDTO the cscpOrgDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cscpOrgDTO,
     * or with status 400 (Bad Request) if the cscpOrgDTO is not valid,
     * or with status 500 (Internal Server Error) if the cscpOrgDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cscpOrgs")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpOrgDTO> updateCscpOrg(@RequestBody CscpOrgDTO cscpOrgDTO) throws URISyntaxException {
        log.debug("REST request to update CscpOrg : {}", cscpOrgDTO);
        if (cscpOrgDTO.getId() == null) {
            return createCscpOrg(cscpOrgDTO);
        }
        CscpOrgDTO result = cscpOrgService.update(cscpOrgDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, cscpOrgDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cscpOrgs : get the cscpOrgs.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpOrgs in body
     */
    @GetMapping("/cscpOrgs")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpOrgDTO> getCscpOrgs(CscpOrgDTO cscpOrgDTO, Pageable page) {
        log.debug("REST request to get CscpOrgs");
        return cscpOrgService.findByCscpOrgDTO(cscpOrgDTO, page);
    }
    
    /**
     * GET  /cscpOrgs : get the cscpOrgs.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpOrgs in body
     */
    @GetMapping("/cscpOrgsByCriteria")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpOrgDTO> getCscpOrgsByCriteria(CscpOrgCriteria cscpOrgCriteria, Pageable page) {
        log.debug("REST request to get CscpOrgsByCriteria");
        return cscpOrgService.findByCscpOrgCriteria(cscpOrgCriteria, page);
    }

    /**
     * GET  /cscpOrgs/:id : get the "id" cscpOrg.
     *
     * @param id the id of the cscpOrgDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cscpOrgDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cscpOrgs/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpOrgDTO> getCscpOrg(@PathVariable Integer id) {
        log.debug("REST request to get CscpOrg : {}", id);
        CscpOrgDTO cscpOrgDTO = cscpOrgService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(cscpOrgDTO));
    }

    /**
     * DELETE  /cscpOrgs/:id : delete the "id" cscpOrg.
     *
     * @param id the id of the cscpOrgDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cscpOrgs/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<Void> deleteCscpOrg(@PathVariable  Integer id) {
        log.debug("REST request to delete CscpOrg : {}", id);
        cscpOrgService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @PostMapping("/cscpOrgs/updateOrg")
    //机构管理拖拽
    public AjaxBackData updateOrgOrderby(@RequestBody CscpOrgDTO[] cscpOrgDtos){
        AjaxBackData ajaxBackData = new AjaxBackData(true, "");
        if (cscpOrgDtos.length <=0){
            return ajaxBackData;
        }else {
            for(CscpOrgDTO cscpOrgDto:cscpOrgDtos){
                if(cscpOrgDto.getOrderby()==0){
                    int orderBy =cscpOrgService.getOrgOrderBy(cscpOrgDto);
                    cscpOrgDto.setOrderby(orderBy);
                }
                cscpOrgService.update(cscpOrgDto);
            }
        }
        return ajaxBackData;
    }
}
